package com.tianqu.tms.www.rental.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.RentalContractInfo;
import com.tianqu.tms.www.rental.action.RentalContractSearchListAction;
import com.tianqu.tms.www.rental.actionform.RentalContractAddInputForm;
import com.tianqu.tms.www.rental.viewhelper.RentalContractAddInputViewHelper;

/**
 * 合同查询/修改-合同一览 controller
 * @author mufeiyu
 */
public class RentalContractSearchListController extends TmsController {
	private Logger logger = Logger.getLogger(RentalContractSearchListController.class);
	public static final String PAGING = "paging";
	public static final String EMPTY = "empty";
	
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Codes codes = Codes.getInstance();
			RentalContractInfo rentalContractInfo = RentalContractInfo.getInstance();
			
			 if(uc.getAttribute(RentalContractSearchListAction.PAGENO) != null) {
				 rentalContractInfo.setPageNo(Integer.parseInt((String)uc.getAttribute(RentalContractSearchListAction.PAGENO)));
				}
				if(uc.getAttribute(RentalContractSearchListAction.MAXINDEX) != null) {
					rentalContractInfo.setMaxIndex(Integer.parseInt((String)uc.getAttribute(RentalContractSearchListAction.MAXINDEX)));
				}
				if(uc.getAttribute(RentalContractSearchListAction.SORTKEY) != null) {
					rentalContractInfo.setSortKey(Integer.parseInt((String)uc.getAttribute(RentalContractSearchListAction.SORTKEY)));
				}
				if(uc.getAttribute(RentalContractSearchListAction.ASC) != null) {
					rentalContractInfo.setAsc((new Boolean((String)uc.getAttribute(RentalContractSearchListAction.ASC))).booleanValue());
				}
			
			List<RentalContractInfo> list = new ArrayList<RentalContractInfo>();
			RentalContractAddInputForm form = (RentalContractAddInputForm)uc.getInputForm();
			// 判定翻页
			if (uc.getAttribute("seniorForm") != null) {
				form = (RentalContractAddInputForm) uc.getAttribute("seniorForm");
			}

			String[] contractStatus = form.getContractStatus();
			list = rentalContractInfo.searchRentalContractInfo(form.getSerial(),form.getLocationSerial(),form.getOwnerName(),
					getContractStatus(contractStatus),form.getStartDateS(),form.getStartDateE(),
					form.getEndDateS(),form.getEndDateE(),form.getRentalArea());
			
			RentalContractAddInputViewHelper viewh = new RentalContractAddInputViewHelper();
			//查询结果判断
			if (null != list && list.size() > 0) {
				List<RentalContractAddInputViewHelper> rentalContractViewHelperlist = new ArrayList<RentalContractAddInputViewHelper>();				
				for (int i = 0; i <list.size(); i++) {
					RentalContractInfo contrctInfo = new RentalContractInfo();
					RentalContractAddInputViewHelper rentalContractViewHelper = new RentalContractAddInputViewHelper();
					contrctInfo = list.get(i);
					rentalContractViewHelper.setId(contrctInfo.getId());
					rentalContractViewHelper.setRowNo(contrctInfo.getRowNo());
					rentalContractViewHelper.setSerial(contrctInfo.getSerial());
					rentalContractViewHelper.setLocationSerial(contrctInfo.getLocationSerial());
					rentalContractViewHelper.setOwnerName(contrctInfo.getOwnerName());
					rentalContractViewHelper.setOwnerIdCardNo(contrctInfo.getOwnerIdCardNo());
					rentalContractViewHelper.setOwnerPhone(contrctInfo.getOwnerPhone());
					rentalContractViewHelper.setStartDate(DateUtil.getDateByTimeStamp(contrctInfo.getStartDate()));
					rentalContractViewHelper.setEndDate(DateUtil.getDateByTimeStamp(contrctInfo.getEndDate()));
					rentalContractViewHelper.setContractStatus(contrctInfo.getContractStatus());
					rentalContractViewHelper.setCssFlag(contrctInfo.getCssFlag()); 
					rentalContractViewHelper.setRentalArea(contrctInfo.getRentalArea()); 
					rentalContractViewHelper.setRentalAreaName(codes.getItemValueByItemIdAndItemCode(Codes.RENTAL_AREA, contrctInfo.getRentalArea())); 
					rentalContractViewHelperlist.add(rentalContractViewHelper);
				}
				viewh.setRentalInfoList(rentalContractViewHelperlist);				
			}
			uc.setAttribute(TmsConstants.VIEWHELP, viewh);
			uc.setAttribute("seniorForm", form);
			uc.setAttribute(PAGING, rentalContractInfo);
		 			
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
	/**
	 * 合同状态取得
	 * @return
	 */
	public String getContractStatus(String[] status){
		String contractStatus = "";
		
		if(null != status && status.length > 0){
			for (int i = 0; i < status.length; i++){
				if (i == 0) {
					contractStatus = status[i];
				} else {
					contractStatus = contractStatus + "," +status[i];
				}
			}
		}
		
		return contractStatus;
	}
}